package org.finesys.chat.core.base.document.splitter;

import org.finesys.chat.core.base.document.tokenizer.Tokenizer;

public class DocumentByLineSplitter extends HierarchicalDocumentSplitter {
    public DocumentByLineSplitter(int maxSegmentSizeInChars, int maxOverlapSizeInChars) {
        super(maxSegmentSizeInChars, maxOverlapSizeInChars, (Tokenizer) null, (DocumentSplitter) null);
    }

    public DocumentByLineSplitter(int maxSegmentSizeInChars, int maxOverlapSizeInChars, DocumentSplitter subSplitter) {
        super(maxSegmentSizeInChars, maxOverlapSizeInChars, (Tokenizer) null, subSplitter);
    }

    public DocumentByLineSplitter(int maxSegmentSizeInTokens, int maxOverlapSizeInTokens, Tokenizer tokenizer) {
        super(maxSegmentSizeInTokens, maxOverlapSizeInTokens, tokenizer, (DocumentSplitter) null);
    }

    public DocumentByLineSplitter(int maxSegmentSizeInTokens, int maxOverlapSizeInTokens, Tokenizer tokenizer, DocumentSplitter subSplitter) {
        super(maxSegmentSizeInTokens, maxOverlapSizeInTokens, tokenizer, subSplitter);
    }

    /**
     * 根据空白字符和换行符分割文本为字符串数组。
     *
     * <p>\\s* 匹配零个或多个空白字符（包括空格、制表符等）。
     * <p>\\R 是通用换行符匹配，可识别不同系统的换行符（包括\n、\r、\r\n）。
     * <p>组合后的\\s*\\R\\s*表示匹配：任意空白符 + 换行符 + 任意空白符的序列，用于分割文本中的行。
     *
     * @param text 要分割的文本
     * @return 分割后的字符串数组
     */
    public String[] split(String text) {
        return text.split("\\s*\\R\\s*");
    }

    /**
     * 获取用于连接字符串的分隔符。
     *
     * @return 返回用于连接字符串的分隔符，此处为换行符"\n"。
     */
    public String joinDelimiter() {
        return "\n";
    }

    protected DocumentSplitter defaultSubSplitter() {
        return new DocumentBySentenceSplitter(this.maxSegmentSize, this.maxOverlapSize, this.tokenizer);
    }
}
